import config from './config'

export default (url, data = {}, method = 'GET') => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: config.host + url,
      data,
      method,
      header:{
        Cookie:wx.getStorageSync('cookies')?wx.getStorageSync('cookies'):''
      },
      success: (res) => {
        // console.log("=======",res)
        if (data.isLogin) { // 登录请求
          // 将用户的cookies存入本地
          let reg = /^MUSIC_U=.*/;
          for (var index = 0; index < res.cookies.length; index++) {
            if (reg.test(res.cookies[index])) {
              wx.setStorage({
                key: 'cookies',
                data: res.cookies[index]
              })
              break;
            }
          }

        }
        resolve(res.data);
      },
      fail: (err) => {
        //   console.log("请求失败",err);
        reject(err);
      }
    })
  })
}